package com.free.food.order.controller;

import com.free.food.product.client.ProductClient;
import com.free.food.product.req.DecreaseStockReq;
import com.free.food.product.res.FindProductInfoRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类功能描述：
 *
 * @author：刘富国
 * @createTime：2019/2/14 14:38
 */
@RestController
@Slf4j
@RefreshScope
public class ClientController {

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;
//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private ProductClient productClient;

    @Value("${env}")
    private String env;

    @RequestMapping("/getProductMsg")
    public String getProductMsg() {
//        //1. 第一种方式
//        RestTemplate restTemplate = new RestTemplate();
//        String result1 = restTemplate.getForObject("http://localhost:18001/msg", String.class);
//        log.info("第一种方式：RestTemplate_{}", result1);
//
//        //2. 第二种方式 注入loadBalancerClient
//        String url = loadBalancerClient.choose("PRODUCT").getHost() + "/msg";
//        log.info("第二种方式:url={}", url);
//        String result2 = restTemplate.getForObject(url, String.class);
//        log.info("第二种方式：LoadBalancerClient_{}", result2);

        //3.第三种方式  注解@LoadBalanced
//        String result3 = restTemplate.getForObject("http://PRODUCT/msg", String.class);
//        log.info("第三种方式：@LoadBalanced_{}", result3);

        String result4 = productClient.productMsg();
        log.info("第四种方式：feign_{}", result4);

        return "MSG";

    }

    @RequestMapping("/msg")
    public String msg() {
        return "msg";
    }


    /**
     * 获取商品列表
     *
     * @return
     */
    @PostMapping("/getProductList")
    public List<FindProductInfoRes> getProductList() {
        return productClient.listForOrder(Arrays.asList("157875196366160022", "164103465734242707"));
    }

    /**
     * 获取商品列表
     *
     * @return
     */
    @GetMapping("/productDecreaseStock")
    public String productDecreaseStock() {
        List<DecreaseStockReq> cartDTOList = new ArrayList<>();
        cartDTOList.add(new DecreaseStockReq("157875196366160022", 2));
        cartDTOList.add(new DecreaseStockReq("157875227953464068", 100));
        productClient.decreaseStock(cartDTOList);
        return "{\"status\":\"ok\"}";
    }

    @GetMapping("/getEnv")
    public String getEnv() {
        return env;
    }


}
